package jdbc2;

import com.tedu.InputUtil;

import java.sql.Connection;
import java.sql.Statement;
import java.util.Scanner;

/*
* 修改密码
* */
public class UpdatePWD {
    public static void main(String[] args) {
        UserInfo userInfo = InputUtil.getInputObject(new UserInfo(),"欢迎修改密码","修改密码");
        try (Connection conn = DBUtil.getConnection()) {
            Statement stmt = conn.createStatement();
            /*
            * 修改密码,前提是用户名存在,且密码正确
            * UPDATE userinfo SET password = '666666' WHERE username = 'EEBond' AND password = '123456';
            * */
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入新密码:");
            String newPwd = sc.nextLine();

            String sql = "UPDATE userinfo SET password = '"+ newPwd+"' " +
                    "WHERE username = '"+ userInfo.getUsername()+"' AND password = '"+ userInfo.getPassword()+"';";
            int num = stmt.executeUpdate(sql);
            System.out.println(num > 0 ? "修改成功":"修改失败,用户名或密码错误");
            System.out.println(num);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
